package com.keyinong.caizhengting.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.adapter.PopwindowAdapter;

import java.util.ArrayList;

/**
 * 团队订餐选择支付方式界面
 */

public class ChoosepayActivity extends Activity {

    ListView list_pupop2 = null;
    ArrayList<String> typelist2;
    /**
     * 价格
     */
    private TextView tv_price;
    private String price;
    /**
     * 返回
     */
    private ImageButton ib_back;
    /**
     * 支付方式
     */
    private TextView tv_zhifu;
    private String str = "微信";
    private String paymode;
    private RelativeLayout rl_paymodel;
    private PopupWindow zhifuTypePopup;
    /**
     * 支付
     */
    private Button btn_pay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_choosepay);
        ib_back = (ImageButton) findViewById(R.id.ib_fanhui9);
        ib_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        String id = getIntent().getStringExtra("orderid2");
        Log.i("shi", "id---" + id);
        tv_price = (TextView) findViewById(R.id.product_price);
        price = getIntent().getStringExtra("total");
        tv_price.setText("￥" + price + "元");
        SharedPreferences spReg = getSharedPreferences("pay",
                Context.MODE_PRIVATE);
        Editor editor = spReg.edit();
        editor.putString("id", id);
        editor.putString("price", price);
        editor.commit();
//		sp=(Spinner)findViewById(R.id.sp_pay);
//		pay();
        tv_zhifu = (TextView) findViewById(R.id.tv_zhifu);
        rl_paymodel = (RelativeLayout) findViewById(R.id.rl_paymodel);
        rl_paymodel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showPopwindow2(rl_paymodel);
            }
        });

        btn_pay = (Button) findViewById(R.id.btn_pay);
        btn_pay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (str.equals("支付宝")) {
                    Intent intent = new Intent();
//					intent.setClass(getApplicationContext(), PayActivity.class);
                    startActivity(intent);
                    paymode = "1";
                } else {
                    Toast.makeText(getApplicationContext(), "微信支付", Toast.LENGTH_LONG).show();
                    paymode = "2";
                }
            }
        });
    }

    /**支付方式*/
//	private void pay() {
//		int layout=R.layout.spinner;
//		int array=R.array.pay;
//		ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, array,layout);
//		/**设置下拉列表布局对象*/
//		adapter.setDropDownViewResource(R.layout.drop_select_item_layout_2);
//		sp.setAdapter(adapter);
//		str = (String) sp.getSelectedItem();
//		sp.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> parent, View view,
//					int position, long id) {
//				str = (String) sp.getSelectedItem();  
//				Log.i("zq", "str---"+str);
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> parent) {
//				// TODO Auto-generated method stub
//
//			}
//		});
//		//		Toast.makeText(getApplicationContext(), obj.toString(), 0).show();
//		//		;
//	}

    /**
     * 支付方式
     */
    public void showPopwindow2(View vi) {
        int with = tv_zhifu.getWidth();

        if (zhifuTypePopup == null) {
            View ppview = LayoutInflater.from(getApplicationContext()).inflate(
                    R.layout.pupop_edittext, null);
            list_pupop2 = (ListView) ppview.findViewById(R.id.list_pupop);

            zhifuTypePopup = new PopupWindow(ppview, 130, 160, true);
        }

        // popupwindow弹出时的动画
        zhifuTypePopup.setAnimationStyle(R.style.popupWindowAnimation);
        typelist2 = new ArrayList<String>();
        typelist2.add("微信");
        typelist2.add("支付宝");

        PopwindowAdapter zhifuadapter = new PopwindowAdapter(typelist2,
                getApplicationContext());

        list_pupop2.setAdapter(zhifuadapter);

        zhifuTypePopup.setBackgroundDrawable(new BitmapDrawable());
        zhifuTypePopup.setFocusable(true);

        // 显示的位置为:屏幕的宽度的1/16
        int xPos = with;

        zhifuTypePopup.showAsDropDown(tv_zhifu);
        list_pupop2.setOnItemClickListener(new PpOnitentClick2());
    }

    /**
     * 支付方式list
     */
    private class PpOnitentClick2 implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            tv_zhifu.setText(typelist2.get(position));
            str = typelist2.get(position);
            Log.i("zq", "str---" + str);
            zhifuTypePopup.dismiss();
        }

    }

}
